package com.wolf.cx330.main.adapter

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ScreenUtils
import com.wolf.cx330.R
import com.wolf.cx330.base.getRandColorCode
import com.wolf.cx330.config.Constants

/**
 * 主界面中的Adapter
 */
class MainAdapter(var context: Context,var onItemClick: (Int, String) -> Unit) :
    RecyclerView.Adapter<MainAdapter.MainViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainViewHolder =
        MainViewHolder(
            LayoutInflater.from(
                context
            ).inflate(R.layout.item_main, parent, false)
        )


    override fun getItemCount(): Int = Constants.MENU_LIST.size

    override fun onBindViewHolder(holder: MainViewHolder, pos: Int) {
        val params = holder.itemView.layoutParams
        params.height = ScreenUtils.getScreenHeight() / 8
        holder.itemView.layoutParams = params
        val model = Constants.MENU_LIST[pos]
        holder.apply {
            itemTv.text = model
            itemTv.setBackgroundColor(Color.parseColor(getRandColorCode()))
            itemTv.setOnClickListener {
                onItemClick(pos,model)
            }
        }
    }

    class MainViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val itemTv: TextView = itemView.findViewById(R.id.itemTv)
    }

}